//
//  setupViewController.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/12/16.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "setupViewController.h"
#import "adviceVC.h"
#import "BaseWebViewController.h"
#import "BPhttpTool.h"
#import "MBProgressHUD+MJ.h"
#import "SetTableViewCell.h"
#import "XieYiViewController.h"
#import "MiMaViewController.h"

#define HMAccountFilepath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.data"]

@interface setupViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tabel;

@property (nonatomic, strong) NSArray *arrayImage;
@property (nonatomic, strong) NSArray *arrayText;
@property (nonatomic, strong) UIWebView *web;
@end

@implementation setupViewController

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.arrayImage = [NSArray array];
    self.arrayText = [NSArray array];
    
    self.arrayImage = [NSArray arrayWithObjects:@"FuWu",@"YiJian",@"XiuGai",@"GengXin", nil];
    self.arrayText = [NSArray arrayWithObjects:@"服务协议",@"意见反馈",@"修改密码",@"检查更新", nil];
    
    
    [self create];
    [self createTabel];
//    UIButton *backAccount = [UIButton buttonWithType:UIButtonTypeCustom];
//    [backAccount setTitle:@"退出账号" forState:UIControlStateNormal];
//    backAccount.size = CGSizeMake(106.0f, 32.0f);
//    backAccount.center = CGPointMake(BPScreenW / 2.0, 300.0f);
//    backAccount.backgroundColor = BPGlobaThemeColor;
//    [backAccount.layer setCornerRadius:16.0f];
//    [backAccount addTarget:self action:@selector(backAccount) forControlEvents:UIControlEventTouchUpInside];
//    [self.baseWebView addSubview:backAccount];
    // Do any additional setup after loading the view.
}

- (void)create{
    
    UIButton *buttonBig = [[UIButton alloc] initWithFrame:CGRectMake(20, 25, 60, 50)];
    buttonBig.backgroundColor = [UIColor clearColor];
    [buttonBig addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonBig];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 23, 23)];
    [button setBackgroundImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBig addSubview:button];
    
    //    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 25, 23, 23)];
    //    [button setBackgroundImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    //    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view addSubview:button];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 30, 20, 100, 30)];
    name.text = @"设置";
    name.textColor = BPFenHongColor;
    name.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:name];
    
    // 波纹线
    UIImageView *bowen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 7)];
    bowen.image = [UIImage imageNamed:@"波纹线"];
    [self.view addSubview:bowen];
}

- (void)back:(UIButton *)bu{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)button:(UIButton *)but{
    [self.navigationController popViewControllerAnimated:YES];
}


// 创建 tabel
- (void)createTabel{
    self.tabel = [[UITableView alloc] initWithFrame:CGRectMake(0, 72, self.view.frame.size.width, self.view.frame.size.height - 72) style:UITableViewStylePlain];
    self.tabel.delegate = self;
    self.tabel.dataSource = self;
    self.tabel.rowHeight = 80;
    [self.view addSubview:self.tabel];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayText.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentify = @"设置";
    SetTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[SetTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tagIma.image = [UIImage imageNamed:self.arrayImage[indexPath.row]];
    cell.label.text = self.arrayText[indexPath.row];
    cell.back.image = [UIImage imageNamed:@"next"];
    
    if (indexPath.row == 3) {
        
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        //CFShow(infoDictionary);
        // app版本
        NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
        // app build版本
        NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
        NSString *banBen = [NSString stringWithFormat:@"%@%@", @"当前版本:V",app_build];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.width - 150, cell.frame.size.height / 2 + 3, 100, 30)];
//        label.text = @"当前版本:V1.2";
        label.text = banBen;
        label.textColor = BPGlobaThemeColor;
        label.font = [UIFont systemFontOfSize:12];
        [cell addSubview:label];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        XieYiViewController *xieyi = [[XieYiViewController alloc] init];
        
        NSString *subDic = @"http://ttmy.len.so/app/appapi/fwxy";
        NSURL *url = [NSURL URLWithString:subDic];
        xieyi.web = [[UIWebView alloc] initWithFrame:self.view.bounds];
        [xieyi.view addSubview:self.web];
        //字体变小
        [xieyi.web setScalesPageToFit:YES];
        
        [xieyi.web loadRequest:[NSURLRequest requestWithURL:url]];
        
        [self.navigationController pushViewController:xieyi animated:YES];
    }
    if (indexPath.row == 1) {
        adviceVC *fankui = [[adviceVC alloc] init];
        [self.navigationController pushViewController:fankui animated:YES];
    }
    if (indexPath.row == 2) {
        MiMaViewController *mima = [[MiMaViewController alloc] init];
        [self.navigationController pushViewController:mima animated:YES];
    }
    if (indexPath.row == 3) {
        [BPhttpTool get:[NSString stringWithFormat:@"%@appapi/checkupdate/type/ios",BPhttp] params:nil success:^(id responseObj) {
            NSDictionary *dic = responseObj;
            NSString *version = dic[@"version"];
            NSString *versionKey = (__bridge NSString *) kCFBundleVersionKey;
            NSString *currentVersion = [NSBundle mainBundle].infoDictionary[versionKey];
            if ([currentVersion floatValue] >= [version floatValue]) {
                [MBProgressHUD showSuccess:@"已是最新版本"];
            }else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"检测到新版本，是否前往下载" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alert show];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"获取版本号失败"];
        }];

    }
}



//
//- (void)backAccount{
//    NSFileManager *defaultManager = [NSFileManager defaultManager];
//    if ([defaultManager isDeletableFileAtPath:HMAccountFilepath]) {
//        [defaultManager removeItemAtPath:HMAccountFilepath error:nil];
//        [self.navigationController popToRootViewControllerAnimated:YES];
//    }
//}
//
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}
//
//- (void)pushAUiViewController:(NSString *)webName
//{
//    BaseWebViewController *webvc = [[BaseWebViewController alloc] init];
//    webvc.webName = webName;
//    webvc.title = @"服务协议";
//    [self.navigationController pushViewController:webvc animated:YES];
//}
//
//- (void)gotoAdvice
//{
//    adviceVC *advc = [[adviceVC alloc] init];
//    [self.navigationController pushViewController:advc animated:YES];
//}
//
//- (void)checkAppVersion
//{
//    [BPhttpTool get:[NSString stringWithFormat:@"%@appapi/checkupdate/type/ios",BPhttp] params:nil success:^(id responseObj) {
//        NSDictionary *dic = responseObj;
//        NSString *version = dic[@"version"];
//        NSString *versionKey = (__bridge NSString *) kCFBundleVersionKey;
//        NSString *currentVersion = [NSBundle mainBundle].infoDictionary[versionKey];
//        if ([currentVersion floatValue] >= [version floatValue]) {
//            [MBProgressHUD showSuccess:@"已是最新版本"];
//        }else{
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"检测到新版本，是否前往下载" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//            [alert show];
//        }
//    } failure:^(NSError *error) {
//        [MBProgressHUD showError:@"获取版本号失败"];
//    }];
//}
//
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    if (buttonIndex == 1) {
//       
//    }
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
